﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipper.Model;
using System.Threading;
using Global;

using Communication;
using Clipper.Model.EnumModel;

namespace Clipper.BLL.DetectChain
{
    /// <summary>
    /// 洗涤，有两个洗涤位
    /// </summary>
    public class WashingTank:ApplianceComponent
    {
        private static Arm mArmWA = new Arm("WA", "12");                    // 洗板臂1
        private static Arm mArmWB = new Arm("WB", "12");                    // 洗板臂2

        private static ControlStatusEnum mStatusWA = ControlStatusEnum.IDLE;        // 洗板臂1的状态
        private static ControlStatusEnum mStatusWB = ControlStatusEnum.IDLE;        // 洗板臂2的状态

        #region ------属性----------- Anry
        /// <summary>
        /// 洗板臂A
        /// </summary>
        public static Arm ArmWA
        {
            get { return mArmWA; }
            set { mArmWA = value; }
        }

        /// <summary>
        /// 洗板臂B
        /// </summary>
        public static Arm ArmWB
        {
            get { return mArmWB; }
            set { mArmWB = value; }
        }
        #endregion ------属性-----------

        #region 变量和属性
        static ControlStatusEnum detectionStatus;
        /// <summary>
        /// 状态
        /// </summary>
        public override ControlStatusEnum DetectionStatus
        {
            get
            {
                //if (AddReagent2.CurrentStrip == null || AddReagent2.CurrentStrip.DetectionMethod == DetectionMethodEnum.ONE_STEP_METHOD)
                //{
                    if (washTank1Status == ControlStatusEnum.IDLE || washTank2Status == ControlStatusEnum.IDLE)
                        return ControlStatusEnum.IDLE;
                    else
                        return ControlStatusEnum.BUSY;
                //}
                //else//两步法预留了位置
                //{
                //    if (washTank1Status == ControlStatusEnum.FINISHED || washTank2Status == ControlStatusEnum.FINISHED)
                //        return ControlStatusEnum.FINISHED;
                //    else
                //        return ControlStatusEnum.BUSY;
                //}
            }
            set
            {
                detectionStatus = value;
            }
        }

        static ControlStatusEnum washTank1Status;
        /// <summary>
        /// 1号洗涤位
        /// </summary>
        public static ControlStatusEnum WashTank1Status
        {
            get
            {
                return washTank1Status;
            }
            set
            {
                washTank1Status = value;
            }
        }
        static ControlStatusEnum washTank2Status;
        /// <summary>
        /// 2号洗涤位
        /// </summary>
        public static ControlStatusEnum WashTank2Status
        {
            get
            {
                return washTank2Status;
            }
            set
            {
                washTank2Status = value;
            }
        }
        static AutoResetEvent mPause = new AutoResetEvent(false);//暂停
        static bool mIsPause;
        /// <summary>
        /// 获得或设置模块暂停状态
        /// </summary>
        public static bool IsPause
        {
            get
            {
                return mIsPause;
            }
        }
        /// <summary>
        /// 洗板1
        /// </summary>
        public static WashControl Wash1 = new WashControl(Can.R2_SUB_ID,"HQ","HU","EA","EB",29,7);
        /// <summary>
        /// 洗板2
        /// </summary>
        public static WashControl Wash2 = new WashControl(Can.R2_SUB_ID, "HY","HV","EC","EF",21,6);
        #endregion

        public override void Run(ReactionCup strip)
        {
            //strip.Note = this.GetType().Name + Thread.CurrentThread.Name + "--" + strip.ID;
            //Console.WriteLine(strip.Note);

            //if (ServiceProvider.IsSimulate)
            //{
            //    Simulate(strip);
            //}
            //else
            //{
            //    // 如果反应条被送到洗板1的等待位
            //    if (strip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK1)
            //    {
            //        DetectChainHelper.Position(73);
            //        ServiceProvider.Monitor.W1Position = strip;

            //        Wash1.WaitStatus(Wash1.MoveLittle());
            //        Wash1.WaitStatus(Wash1.WashDown());
            //        Wash1.WashOut(4000);
            //        Wash1.WaitStatus(Wash1.WashDown_1());
            //        Wash1.WaitStatus(Wash1.MoveLittle());
            //        Wash1.WaitStatus(Wash1.WashDown());
            //        Wash1.WashOut(4000);

            //        Wash1.WaitStatus(Wash1.WashDown_1());
            //        Wash1.WaitStatus(Wash1.MoveLittle());
            //        for (int i = 0; i < 5; i++)
            //        {
            //            //Wash1.WashIn();
            //            //Wash1.WaitStatus(Wash1.WashDown());
            //            //Wash1.WashOut();
            //            //Wash1.WaitStatus(Wash1.WashDown_1());

            //            Wash1.WashIn(2000);
            //            Thread.Sleep(15000);
            //            Wash1.WaitStatus(Wash1.WashDown());
            //            Wash1.WashOut(2000);
            //            Wash1.WaitStatus(Wash1.WashDown_1());

            //            Wash1.WaitStatus(Wash1.MoveLittle());
            //            Wash1.WaitStatus(Wash1.WashDown());
            //            Wash1.WashOut(2000);
            //            Wash1.WaitStatus(Wash1.WashDown_1());
            //            Wash1.WaitStatus(Wash1.MoveLittle());

            //        }
                  
            //        Wash1.WaitStatus(Wash1.WashUp());
            //    }
            //    else if (strip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK2)
            //    {
            //        DetectChainHelper.Position(74);
            //        ServiceProvider.Monitor.W2Position = strip;

            //        #region 老的
            //        //Wash2.WaitStatus(Wash2.MoveLittle());
            //        //Wash2.WaitStatus(Wash2.WashDown());
            //        //Wash2.WashOut();
            //        //for (int i = 0; i <5; i++)
            //        //{
            //        //    Wash2.WaitStatus(Wash2.WashDown_1());
            //        //    Wash2.WashIn();
            //        //    Thread.Sleep(10000);
            //        //    Wash2.WaitStatus(Wash2.WashDown());
            //        //    if (i == 4)
            //        //    {
            //        //        Wash2.WashOutLast();
            //        //    }
            //        //    else
            //        //    {
            //        //        Wash2.WashOut();
            //        //    }
            //        //   Wash2.WaitStatus(Wash2.WashDown_1());
            //        //}
            //        //Wash2.WaitStatus(Wash2.MoveLittle());
            //        //Wash2.WaitStatus(Wash2.WashDown());
            //        //Wash2.WashOutLast();
            //        #endregion

            //        //Wash2.WaitStatus(Wash2.WashDown());
            //        //Wash2.WashOut(2000);

            //        Wash2.WaitStatus(Wash2.MoveLittle());
            //        Wash2.WaitStatus(Wash2.WashDown());
            //        Wash2.WashOut(4000);
            //        Wash2.WaitStatus(Wash2.WashDown_1());
            //        Wash2.WaitStatus(Wash2.MoveLittle());
            //        Wash2.WaitStatus(Wash2.WashDown());
            //        Wash2.WashOut(4000);

            //        //Wash2.WashIn(2000);
            //        //Wash2.WashOut(4000);

            //        Wash2.WaitStatus(Wash2.WashDown_1());
            //        Wash2.WaitStatus(Wash2.MoveLittle());
            //        for (int i = 0; i < 5; i++)
            //        {

            //            Wash2.WashIn(2000);
            //            Thread.Sleep(15000);
            //            Wash2.WaitStatus(Wash2.WashDown());
            //            Wash2.WashOut(2000);
            //            Wash2.WaitStatus(Wash2.WashDown_1());

            //            Wash2.WaitStatus(Wash2.MoveLittle());
            //            Wash2.WaitStatus(Wash2.WashDown());
            //            Wash2.WashOut(2000);
            //            Wash2.WaitStatus(Wash2.WashDown_1());
            //            Wash2.WaitStatus(Wash2.MoveLittle());

            //        }


            //        Wash2.WaitStatus(Wash2.WashUp());
                    
            //    }
            //    else
            //    {
            //        Console.WriteLine("--------------洗涤出错-------------"+strip.ReactionStripSeat.ToString());
            //    }

            //}
            //Transportation.AddTransportation(strip, this, NextComponent);
        }
        /// <summary>
        /// 暂停/恢复
        /// </summary>
        /// <param name="isPause">暂停true,恢复false</param>
        public static void Pause(bool isPause)
        {
            mIsPause = isPause;
            Wash1.IsPause = isPause;
            Wash2.IsPause = isPause;
        }
        public override void Simulate(ReactionCup strip)
        {
            Thread.Sleep(ServiceProvider.WashTankSleepTime);
        }
        public static void RunEngineer(ReactionStrip strip)
        {
            
             // 如果反应条被送到洗板1的等待位
            if (strip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK1)
            {
                Transportation.TransportD.WaitStatus(Transportation.TransportD.WashPosition());
                Wash1.WaitStatus(Wash1.WashDown());

            }
            else
            {
                Transportation.TransportE.WaitStatus(Transportation.TransportE.WashPosition());
                Wash2.WaitStatus(Wash2.WashDown());
            }
        }

        #region // 模块运行控制，Anry
        private void RunControl(ReactionStrip strip)
        {
            // 如果反应条被送到洗板1的等待位
            if (strip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK1)
            {
                // 如果模块需要暂停，则暂停模块运行，等待外部改变此状态且继续模块运行
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                // 滑轨D将反应条从等待位移到洗板位，将滑轨D的状态设置为忙
                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    Transportation.SlideMovingToFeatureSite('D');
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                // 循环等待，直到滑轨D状态为空闲，跳出循环，以保证反应条被移到洗板位
                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                // 如果清洗液剩余量达到警告值，则弹出警告界面

                // 如果清洗液剩余量达到临界值，则弹出提示框，告知用户

                // 如果清洗液剩余量大于临界值
                // 如果洗板臂1空闲,则使用它进行洗板
                if (mStatusWA == ControlStatusEnum.IDLE)
                {
                    mArmWA.WashStrip(1);
                    mStatusWA = ControlStatusEnum.BUSY;                   
                }

                // 循环等待，直到洗板臂1的状态为空闲，以保证洗板完成
                while (mStatusWA == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }
            else     // 如果反应条被送到洗板2的等待位
            {
                // 如果模块需要暂停，则暂停模块运行，等待外部改变此状态且继续模块运行
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                // 滑轨E将反应条从等待位移到洗板位，将滑轨E的状态设置为忙
                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    Transportation.SlideMovingToFeatureSite('E');
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                // 循环等待，直到滑轨D状态为空闲，跳出循环，以保证反应条被移到洗板位
                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                // 如果清洗液剩余量达到警告值，则弹出警告界面

                // 如果清洗液剩余量达到临界值，则弹出提示框，告知用户

                // 如果清洗液剩余量大于临界值
                // 如果洗板臂1空闲,则使用它进行洗板
                if (mStatusWB == ControlStatusEnum.IDLE)
                {
                    mArmWB.WashStrip(2);
                    mStatusWB = ControlStatusEnum.BUSY;

                    // 循环等待，直到洗板臂1的状态为空闲，以保证洗板完成
                    while (mStatusWB == ControlStatusEnum.BUSY)
                    {
                        System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                    }
                }
            }
        }
        #endregion // 模块运行控制，Anry
    }
}
